# ============================================================================
# Helloworld Server Subproject
# ============================================================================
# Note: Common configurations are inherited from parent CMakeLists.txt:
#   - CMake version, C++ standard
#   - TRPC paths, include/link directories
#   - PB_PROTOC, TRPC_CPP_PLUGIN, LIBRARY variables
# ============================================================================

# ============================================================================
# Helloworld Server Executable
# ============================================================================
add_executable(helloworld_svr 
    ${CMAKE_CURRENT_SOURCE_DIR}/helloworld_server.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/greeter_service.cc
)

target_link_libraries(helloworld_svr 
    PRIVATE
        service_common 
        ${LIBRARY}
)

# Set RPATH for runtime library search
set_target_properties(helloworld_svr PROPERTIES
    INSTALL_RPATH "$ORIGIN/lib"
    BUILD_WITH_INSTALL_RPATH FALSE
    INSTALL_RPATH_USE_LINK_PATH TRUE
)
# ============================================================================
# Greeter Service Test Executable
# ============================================================================
add_executable(greeter_service_test 
    ${CMAKE_CURRENT_SOURCE_DIR}/greeter_service_test.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/greeter_service.cc
)

# Note: ${LIB_GTEST_GMOCK} must be linked before ${LIBRARY}
target_link_libraries(greeter_service_test 
    PRIVATE
        service_common 
        ${LIB_GTEST_GMOCK} 
        ${LIBRARY}
)

# Set RPATH for runtime library search
set_target_properties(greeter_service_test PROPERTIES
    INSTALL_RPATH "$ORIGIN/lib"
    BUILD_WITH_INSTALL_RPATH FALSE
    INSTALL_RPATH_USE_LINK_PATH TRUE
)

# Enable testing
enable_testing()
add_test(NAME greeter_service_test COMMAND greeter_service_test)
# ============================================================================
# MongoDB Test Executable
# ============================================================================
add_executable(mongotest_svr 
    ${CMAKE_CURRENT_SOURCE_DIR}/mongotest.cc
)

target_link_libraries(mongotest_svr 
    PRIVATE
        mongocxx 
        bsoncxx 
        ${LIBRARY}
)

# Set RPATH for runtime library search
set_target_properties(mongotest_svr PROPERTIES
    INSTALL_RPATH "$ORIGIN/lib"
    BUILD_WITH_INSTALL_RPATH FALSE
    INSTALL_RPATH_USE_LINK_PATH TRUE
)
# ============================================================================
# Installation Rules
# ============================================================================
install(TARGETS helloworld_svr 
        RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/../runtime/bin/helloworld)

install(TARGETS greeter_service_test 
        RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/../runtime/bin/helloworld)

install(TARGETS mongotest_svr 
        RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/../runtime/bin/mongotest_svr)
